import {Component, EventEmitter, Input, Output, ViewChild} from '@angular/core';
import {Slides} from "ionic-angular";

@Component({
  selector: 'g-slides',
  templateUrl: 'g-slides.html'
})
export class GSlidesComponent {

  @ViewChild('slides') slides: Slides
  @Input() index: number
  @Input() childrenNumber: number = 2
  @Input() disabledPointer: boolean
  @Output() didChange = new EventEmitter()

  slidesMoving: boolean
  slidesHeight: any
  _disabledPointer: boolean

  constructor() {
  }

  slidesDidChange() {
    this.slidesMoving = false;
    let slideIndex: number = Math.min(this.slides.getActiveIndex(),this.childrenNumber-1)
    this.didChange.emit(slideIndex)
  }

  slideWillChange() {
    this.slidesMoving = true;
  }

  ngOnChanges(changes) {
    let indexObj = changes.index
    if (indexObj && !indexObj.firstChange && this.slides) {
      this.slides.slideTo(indexObj.currentValue)
    }
    let disabledPointerObj = changes.disabledPointer
    if (disabledPointerObj) {
      this._disabledPointer = !disabledPointerObj.currentValue
    }
  }

}
